package com.ckzp.jfinal.plugins.tree;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 51594 on 2022/7/27.
 */
public class TreeNode {

    //节点唯一索引值，用于对指定节点进行各类操作
    public String id;
    //节点标题
    public String title;
    //标题的文本信息
    public String text;
    //节点字段名
    public String field;
    //点击节点弹出新窗口对应的 url。需开启 isJump 参数
    public String href;
    //节点是否初始展开，默认 false
    public boolean spread = false;
    //节点是否初始为选中状态（如果开启复选框的话），默认 false
    public boolean checked = false;
    //节点是否为禁用状态。默认 false
    public boolean disabled = false;
    //默认允许跳转
    public boolean turn = true;

    public List children = new ArrayList();

    public Map datas = new HashMap();

    public static TreeNode newNode(String id) {
        return new TreeNode(id);
    }


    public boolean hasChild() {
        if (children != null && children.size() > 0) return true;
        return false;
    }

    /**
     *
     * @param nodes
     * @return
     */
    public static List newNodeList(TreeNode... nodes) {
        List treelist = new ArrayList<TreeNode>();
        for (TreeNode node : nodes) {
            treelist.add(node);
        }
        return treelist;
    }


    public static List newNodeList() {
        List treelist = new ArrayList<TreeNode>();
        return treelist;
    }

    public TreeNode(String id) {
        this.id = id;
    }

    public String getId() {
        return id;
    }

    public TreeNode setId(String id) {
        this.id = id;
        return this;

    }

    public String getTitle() {
        return title;
    }

    public TreeNode setTitle(String title) {
        this.text = title;
        this.title = title;
        return this;
    }

    public String getField() {
        return field;
    }

    public TreeNode setField(String field) {
        this.field = field;
        return this;
    }

    public String getHref() {
        return href;
    }

    public TreeNode setHref(String href) {
        this.href = href;
        return this;
    }

    public boolean isSpread() {
        return spread;
    }

    public TreeNode setSpread(boolean spread) {
        this.spread = spread;
        return this;
    }

    public boolean isChecked() {
        return checked;
    }

    public TreeNode setChecked(boolean checked) {
        this.checked = checked;
        return this;
    }


    public TreeNode setClassName(String classname) {
        this.title = "<span class=\"" + classname + "\">" + this.text + "</span>";
        return this;
    }


    public TreeNode setColor(String color) {
        this.title = "<span style=\"color:" + color + "\">" + this.text + "</span>";
        return this;
    }


    public TreeNode setStyle(String style) {
        this.title = "<span style=\"" + style + "\">" + this.text + "</span>";
        return this;
    }

    public boolean isDisabled() {
        return disabled;
    }

    public TreeNode setDisabled(boolean disabled) {
        this.disabled = disabled;
        return this;
    }

    public List getChildren() {
        return children;
    }

    public TreeNode setChildren(List children) {
        this.children = children;
        return this;
    }

    public Map getDatas() {
        return datas;
    }

    public TreeNode setDatas(Map datas) {
        this.datas = datas;
        return this;
    }

    public TreeNode addDatas(String key, Object values) {
        this.datas.put(key, values);
        return this;
    }

    public boolean isTurn() {
        return turn;
    }

    public TreeNode setTurn(boolean turn) {
        this.turn = turn;
        return this;
    }

    public void addChildren(TreeNode node) {
        if (!hasChild()) node.setSpread(true);
        this.children.add(node);
    }

    public String toJson() {
        return JSON.toJSONString(this);
    }
}
